<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="buyOrderCode">
        <el-input v-model="queryParams.buyOrderCode" placeholder="请输入采购订单编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料名称" prop="mtrName">
        <el-input v-model="queryParams.mtrName" placeholder="请输入物料名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="采购数量" prop="buyQuantity">
        <el-input v-model="queryParams.buyQuantity" placeholder="请输入采购数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="供应商" prop="supplier">
        <el-input v-model="queryParams.supplier" placeholder="请输入供应商" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:order:add']">新增</el-button>
      </el-col>
 
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:order:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mes:order:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="采购订单编号" align="center" prop="buyOrderCode" />
      <el-table-column label="采购产品" align="center" prop="mtrNameList" />
      <el-table-column label="总价" align="center" prop="budgetedCost" >
        <template slot-scope="scope">
          {{ (scope.row.budgetedCost).toFixed(2)+'元' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="tagType(scope.row.status)" effect="dark">
            <dict-tag :options="dict.type.mes_order_arrival" :value="scope.row.status" />
          </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:order:edit']">订单明细</el-button>
          <el-button v-if="scope.row.status == 0" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['mes:order:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />



    <!-- 订单明细对话框 -->
    <transition name="el-fade-in-linear">
      <transition name="el-fade-in">

        <el-dialog :title="title" :visible.sync="open" v-if="open" width="750px" append-to-body>

          <label>采购订单编号: {{ Detailsform.length == 0 ? "" : Detailsform[0].buyOrderCode }}</label>
          <el-divider>物料列表</el-divider>


          <el-card id="mtrcard" style="position: relative;" shadow="never" v-for="(item, index) in Detailsform"
            :key="item.id">
            <div style="display: flex;width: 90%;">
              <div style="display: inline-block;flex: 1;text-align: center;">
                <h4 style="font-style:oblique;background-color: aliceblue;padding: 5px;border-radius: 7px;margin-right: 20px;">物料名称</h4>
                <h4 style="font-weight: 500;margin-right: 20px;">{{ item.mtrName }}</h4>
              </div>
              <div style="display: inline-block;flex: 1;text-align: center;">
                <h4 style="font-style:oblique;background-color: aliceblue;padding: 5px;border-radius: 7px;margin-right: 20px;">物料编号</h4>
                <h4 style="font-weight: 500;margin-right: 20px;">{{ item.mtrCode }}</h4>
              </div>
              <div style="display: inline-block;flex: 1;text-align: center;">
                <h4 style="font-style:oblique;background-color: aliceblue;padding: 5px;border-radius: 7px;margin-right: 20px;">到货时间</h4>
                <h4 v-if="!buyOrderExit" style="font-weight: 500;margin-right: 20px;">{{ item.createTime }}</h4>
                <el-date-picker v-else style="width: 190px;" size="mini" v-model="Detailsform[index].createTime" type="date"
                  placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd">
                </el-date-picker>
              </div>
              <div style="display: inline-block;flex: 1;text-align: center;">
                <h4 style="font-style:oblique;background-color: aliceblue;padding: 5px;border-radius: 7px;margin-right: 20px;">供应商</h4>
                <h4 v-if="!buyOrderExit" style="font-weight: 500;margin-right: 20px;">{{ item.supplier }}</h4>
                <el-input v-else v-model="Detailsform[index].supplier" size="mini"></el-input>
              </div>
              <div style="display: inline-block;flex: 1;text-align: center;">
                <h4 style="font-style:oblique;background-color: aliceblue;padding: 5px;border-radius: 7px;margin-right: 20px;">采购数量</h4>
                <h4 v-if="!buyOrderExit" style="font-weight: 500;margin-right: 20px;">{{ item.buyQuantity }}</h4>
                <el-input-number v-else style="width: 100%;" size="mini" v-model="Detailsform[index].buyQuantity"
                  controls-position="right" :min="0"></el-input-number>
              </div>
              <div v-if="!buyOrderExit" style="display: inline-block;flex: 1;text-align: center;">
                <h4 style="font-style:oblique;background-color: aliceblue;padding: 5px;border-radius: 7px;margin-right: 20px;">总价</h4>
                <h4  style="font-weight: 500;margin-right: 20px;">{{ (item.cost).toFixed(2)+'元' }}</h4>
                
              </div>
              <div
                style="display: flex;text-align: center;position: absolute;right: 0;top: 0; height: 100%;width: 80px;align-items: center;justify-content: center;">
                <!-- <el-button v-if="Detailsform[index].status == 0" id="daohuobtn" type="success" 
                   @click="daohuo(Detailsform[index])">确认到货</el-button> -->
                   <button v-if="Detailsform[index].status == 0" @click="daohuo(Detailsform[index])" id="daohuobtn"><i class="el-icon-s-claim"></i>到货</button>
                <el-button v-else :disabled="true" size="mini" type="text" icon="el-icon-edit" @click=""
                  v-hasPermi="['mes:order:edit']">已到货</el-button>
              </div>
            </div>
          </el-card>

          <div slot="footer" class="dialog-footer">

            <el-popconfirm v-if="buyOrderExit" style="margin-right: 10px;" confirm-button-text='确定'
              cancel-button-text='取消' icon="el-icon-info" icon-color="red" title="确定修改吗？" @confirm="buyOrderMtrListExit">
              <el-button type="primary" @click="" slot="reference">保 存</el-button>
            </el-popconfirm>

            <el-button v-else :disabled="noExit" type="primary" @click="buyOrderExit = true;">编 辑</el-button>
            <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </transition>
    </transition>


    <!-- 订单添加对话框 -->
    <el-dialog :title="title" :visible.sync="addopen" v-if="addopen" width="971px" append-to-body>
      <div style="margin-bottom: 10px;">
        选择物料:
        <el-select v-model="showValue" filterable placeholder="请选择" @change="mtrChange">
          <el-option v-for="item in mtrOptions" :key="item.id" :label="item.mtrName" :value="item.mtrCode">
          </el-option>
        </el-select>
      </div>

      <el-table :data="addform" border style="width: 100%;border-radius: 15px;">
        <el-table-column prop="mtrName" label="物料名称" width="180">
        </el-table-column>
        <el-table-column prop="mtrCode" label="物料编码" width="180">
        </el-table-column>
        <el-table-column prop="buyQuantity" label="采购数量" width="180">
          <template slot-scope="scope">
            <el-input-number size="mini" v-model="addform[scope.$index].buyQuantity"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column prop="supplier" label="供应商" width="180">
          <template slot-scope="scope">
            <el-input size="mini" v-model="addform[scope.$index].supplier"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="delMtr(scope.$index)"
              v-hasPermi="['mes:mtrinfo:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, arrival } from "@/api/mes/order";
import { listMtrinfo } from "@/api/mes/mtrinfo";
export default {
  name: "Order",
  dicts: ['mes_order_arrival'],
  data() {
    return {
      //下拉框展示值
      showValue: "",
      //物料下拉框数据
      mtrOptions: [],
      /**
       * 禁止编辑
       */
      noExit: false,
      /**
       * 采购订单明细编辑状态
       */
      buyOrderExit: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //添加对话框
      addopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buyOrderCode: null,
        mtrName: null,
        buyQuantity: null,
        supplier: null,
      },
      // 明细对话框参数
      Detailsform: {},
      //添加对话框参数
      addform: {},
      // 表单校验
      rules: {
      }
    };
  },
  computed: {
    //是否填写了供应商
    haveSupplier() {
      let flag = false;
      this.addform.forEach(e => {
        if (e.supplier == null || e.supplier == undefined || e.supplier == "") {
          flag = true
        }
      })
      return flag
    }
  },
  created() {
    this.getList();
  },
  methods: {

    //删除物料
    delMtr(index) {
      this.addform.splice(index, 1)
    },
    //下拉框改变事件
    mtrChange(option) {
      if (this.addform.findIndex(e => e.mtrCode == option) != -1) {
        this.$modal.msgError("无法添加重复的物料");
      } else {
        this.addform.push(this.mtrOptions.find(e => e.mtrCode == option))
      }
      this.showValue = ""
    },
    //标签类型判断
    tagType(status) {
      switch (status) {
        case 0:
          return "info"
        case 1:
          return ''
        case 2:
          return "success"
      }
    },
    //到货
    daohuo(row) {
      arrival({ id: row.id }).then(() => {
        this.open = false;
        this.$modal.msgSuccess("到货成功");
        this.buyOrderExit = false
        this.getList()
      })
    },
    /**
     * 采购订单明细编辑
     */
    buyOrderMtrListExit() {
      updateOrder(this.Detailsform).then((res) => {
        this.buyOrderExit = false
      })
    },
    /** 查询采购订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.orderList.forEach(element => {
          element.mtrNameList = element.mtrNameList.join();
        });
        this.total = response.total;
        this.loading = false;
      });
      listMtrinfo().then(res => {
        this.mtrOptions = res.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.addopen = false;
      this.buyOrderExit = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.Detailsform = {
        id: null,
        needId: null,
        buyOrderCode: null,
        mtrCode: null,
        mtrName: null,
        buyQuantity: null,
        status: null,
        supplier: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.addform = [

      ];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addopen = true;
      this.title = "添加采购订单";

    },
    /** 订单明细按钮操作 */
    handleUpdate(row) {
      // this.reset();
      getOrder({ buyOrderCode: row.buyOrderCode }).then(response => {
        this.buyOrderExit = false
        this.noExit = false
        this.Detailsform = response.data;
        this.Detailsform.forEach(item => {
          if (item.status == 1) {
            this.noExit = true
          }
        })
        this.open = true;
        this.title = "订单明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.haveSupplier == true) {
        return this.$modal.msgError("供应商不能为空");
      }
      addOrder(this.addform).then(res => {
        this.$notify({
          title: '成功',
          message: '新增成功',
          type: 'success',
          position: 'top-left'
        });
        this.getList()
        this.addopen = false
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采购订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
#mtrcard {
  border-radius: 15px;
  background-image: linear-gradient(to top, #c4c5c7 0%, #dcdddf 52%, #ebebeb 100%);
  margin-bottom: 10px;
  box-shadow: 3px 3px 5px rgba(128, 128, 128, 0.292);
  padding-bottom: unset;
}

#daohuobtn {
  height: 45px;
  width: 70px;
  border: unset;
  border-radius: 10px;
  font-weight: 400;
  background-color: rgb(83, 224, 109);
  position: relative;
  align-items: center;
  justify-content: center;
  transition: width 0.3s, height 0.3s, border-radius 0.3s;
}

#daohuobtn:hover {
  height: 100%;
  width: 100%;
  border-radius: unset;
}
</style>
